package br.domsd.pocketcontrol.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import android.content.Context;
import android.util.Log;
import br.domsd.pocketcontrol.model.Period;
import br.domsd.pocketcontrol.utils.Constants;

public class XMLHandler 
{

	private Context context;
	private XMLWriter xmlWriter;
	private XMLReader xmlReader;
	private Document xmlDocument;
	
	public XMLHandler(Context paramContext)
	{
		context = paramContext;
	}
	
	public void createXML(Period period) 
	{
			String fileName = createFileName(period.getMonth(), period.getYear());
			File path = new File(context.getFilesDir(), Constants.DIR_NAME);
			path.mkdirs();
			
			File file = new File(path, fileName);
			
			createDocument();

			xmlWriter = new XMLWriter(file, xmlDocument);
			
			xmlWriter.setupXML(period);
			xmlWriter.write();
			
	}
	
	public void openXML(Period period) 
	{
			String fileName = createFileName(period.getMonth(), period.getYear());
			File path = new File(context.getFilesDir(), Constants.DIR_NAME);
			path.mkdirs();
			
			File file = new File(path, fileName);
			
			openDocument(file);
	}

	public void readXML(Period period)
	{
		String fileName = createFileName(period.getMonth(), period.getYear());
		File path = new File(context.getFilesDir(), "monthExpenses");
		
		File file = new File(path, fileName);
		
		xmlReader = new XMLReader(file);
		xmlReader.read();
	}

	public void addExpense(Period period, String date, String ammount)
	{
		String fileName = createFileName(period.getMonth(), period.getYear());
		File path = new File(context.getFilesDir(), Constants.DIR_NAME);
		path.mkdirs();
		
		File file = new File(path, fileName);
		
		xmlWriter = new XMLWriter(file, xmlDocument);
		xmlWriter.writeExpenseToXML(date, ammount);
		
	}
	
	private void createDocument()
	{
		try
		{
			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder;
			docBuilder = docFactory.newDocumentBuilder();
			xmlDocument = docBuilder.newDocument();
		}
		catch (ParserConfigurationException e)
		{
			Log.e(XMLHandler.class.getSimpleName(), e.getMessage());
		}

		
	}
	
	private void openDocument(File file)
	{
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder;
		try 
		{
			dBuilder = dbFactory.newDocumentBuilder();
			xmlDocument = dBuilder.parse(file);
			xmlDocument.getDocumentElement().normalize();
		}
		catch(ParserConfigurationException e)
		{
			Log.e(XMLHandler.class.getSimpleName(), e.getMessage());
		}
		catch (SAXException e) 
		{
			Log.e(XMLHandler.class.getSimpleName(), e.getMessage());
		}
		catch (IOException e) 
		{
			Log.e(XMLHandler.class.getSimpleName(), e.getMessage());
		}
	}
	
	public String createFileName(String month, String year)
	{
		String fileName = month + "-" + year + ".xml";
		return fileName;
	}
}
